package com.zjsystem.zjsy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjsystem.zjsy.dto.ZjInstruct;
import com.zjsystem.zjsy.dto.ZjxdPlan;
import com.zjsystem.zjsy.enums.InstructEnums;
import com.zjsystem.zjsy.enums.PlanDwEnums;
import com.zjsystem.zjsy.mapper.ZjInstructMapper;
import com.zjsystem.zjsy.request.InstructRequest;
import com.zjsystem.zjsy.service.ZjInstructService;
import com.zjsystem.zjsy.service.ZjxdPlanService;
import com.zjsystem.zjsy.utils.DateTimes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyf
 * @since 2024-07-11
 */
@Slf4j
@Service
public class ZjInstructServiceImpl extends ServiceImpl<ZjInstructMapper, ZjInstruct> implements ZjInstructService {
    @Resource
    private ZjxdPlanService planService;

    @Override
    public IPage<ZjInstruct> pageList(InstructRequest request) {
        String rwId = request.getRwId();
        ZjxdPlan zjxdPlan = planService.getOne(Wrappers.<ZjxdPlan>lambdaQuery().eq(ZjxdPlan::getAssId, rwId));
        //第一次进来 data为null 前段默认进入创建中页面
        if (zjxdPlan == null) {
            throw new RuntimeException("异常Id");
        }
        Long planId = zjxdPlan.getId();
        long num = request.getPage();
        long size = request.getSize();
        Page<ZjInstruct> page = new Page<>(num, size);
        String instructName = request.getInstructName();
        String instructType = request.getInstructType();
        Integer days = request.getDays();
        IPage<ZjInstruct> instructIPage = null;

        LambdaQueryWrapper<ZjInstruct> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(instructName)) {
            qw.like(ZjInstruct::getInstructName, instructName);
        }
        if (StringUtils.isNotBlank(instructType)) {
            qw.eq(ZjInstruct::getInstructType, instructType);
        }
        // 获取N天内的数据
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -days);
        Date agoDate = calendar.getTime();
        qw.eq(ZjInstruct::getPlanId, planId);
        qw.ge(ZjInstruct::getCreateTime, agoDate);
        qw.orderByDesc(ZjInstruct::getCreateTime);
        instructIPage = this.page(page, qw);
        List<ZjInstruct> records = instructIPage.getRecords();
        records.forEach(info -> info.setInstructType(InstructEnums.valueOf(info.getInstructType()).getDesc()));
        return instructIPage;
    }

    @Override
    public void saveInstruct(InstructRequest request) {
        log.info("指令新增入参为：{}", JSON.toJSONString(request));
        String rwId = request.getRwId();
        ZjxdPlan zjxdPlan = planService.getOne(Wrappers.<ZjxdPlan>lambdaQuery().eq(ZjxdPlan::getAssId, rwId));
        if (zjxdPlan == null){
            throw new RuntimeException("任务Id 未查到筹划任务表");
        }
        ZjInstruct instruct = new ZjInstruct();
        BeanUtils.copyProperties(request, instruct);
        instruct.setPlanId(zjxdPlan.getId());
        instruct.setSendTime(DateTimes.now());
        instruct.setSendDwMc(PlanDwEnums.valueOf(request.getSendDwId()).getDesc());
        instruct.setCreateTime(new Date());
        instruct.setUpdateTime(new Date());
        this.save(instruct);

        log.info("指令新增入库为：{}", JSON.toJSONString(instruct));
    }

}
